package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JScrollPane;

import com.definity.toolkit.ui.Body;

public class Scroll extends JScrollPane implements Widget<Scroll> {

	private static final long serialVersionUID = -8827652560941820492L;

	public Scroll(Component component) {
		super(component);
		
		type(ScrollType.VERTICAL_AS_NEEDED).type(ScrollType.HORIZONTAL_NEVER);
	}
	
	@Override
	public String id() {
		return getName();
	}

	@Override
	public Scroll id(String id) {
		setName(id);
		return this;
	}

	@Override
	public Scroll properties(Body<Scroll> body) {
		body.content(this);
		return this;
	}

	@Override
	public Scroll size(int width, int height) {
		setPreferredSize(new Dimension(width, height));
		return this;
	}

	public Scroll type(ScrollType type) {
		switch (type) {
		case BOTH_NEVER:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
			break;
		case BOTH_ALWAYS:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
			break;
		case BOTH_AS_NEEDED:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
			break;
		case VERTICAL_NEVER:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
			break;
		case VERTICAL_ALWAYS:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
			break;
		case VERTICAL_AS_NEEDED:
			setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
			break;
		case HORIZONTAL_NEVER:
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
			break;
		case HORIZONTAL_ALWAYS:
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
			break;
		case HORIZONTAL_AS_NEEDED:
			setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
			break;
		}
		return this;
	}
	
	public enum ScrollType {
		BOTH_NEVER,
		BOTH_ALWAYS,
		BOTH_AS_NEEDED,
		VERTICAL_NEVER,
		VERTICAL_ALWAYS,
		VERTICAL_AS_NEEDED,
		HORIZONTAL_NEVER,
		HORIZONTAL_ALWAYS,
		HORIZONTAL_AS_NEEDED;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public Scroll background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public Scroll foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public Scroll font(Font font) {
		setFont(font);
		return this;
	}
	
}
